#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define SIZE 128

/**
 * 先读后写
 * @return
 */
int main() {
    int writeFd = -1;
    int readFd = -1;
    int ret = -1;
    char buf[SIZE];
    readFd = open("talkfifo1",O_RDONLY);
    if(-1 == readFd){
        perror("open");
        return 1;
    }
    printf("只读的方式打开一个管道文件1\n");
    writeFd = open("talkfifo2",O_WRONLY);
    if(-1 == writeFd){
        perror("open");
        return 1;
    }
    printf("只写的方式打开一个管道文件2\n");
    //写管道
    while(1){
        memset(buf,0,SIZE);
        ret = read(readFd,buf,SIZE);
        if(ret <= 0){
            perror("read");
            break;
        }
        printf("read : %s\n",buf);

        memset(buf,0,SIZE);
        fgets(buf,SIZE,stdin);
        if ('\n' == buf[strlen(buf) - 1])
            buf[strlen(buf) - 1] = '\0';

        ret = write(writeFd,buf, strlen(buf));
        if(ret <= 0){
            perror("write");
            break;
        }
        printf("write ： %s\n",buf);
    }

    //关闭文件描述符
    close(readFd);
    close(writeFd);
    return 0;
}
